package com.kg.sdk.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.TextView;



public class AutoFitTextView extends TextView {
    private final static int DEFAULT_MAX = 35;
    private final static int DEFAULT_MIN = 10;
    public AutoFitTextView(Context context) {
        super(context);
        init(null);
    }

    public AutoFitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        float max = DEFAULT_MAX;
        float min = DEFAULT_MIN;
        if(attrs != null){
          //  TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.AutoFitTextView);
        //    max = ta.getDimension(R.styleable.AutoFitTextView_maxTextSize, DEFAULT_MAX);
          //  min = ta.getDimension(R.styleable.AutoFitTextView_minTextSize, DEFAULT_MIN);
        }
        maxTextSize = this.getTextSize();
        Log.e("tv"," maxTextSize is "+maxTextSize+" max is "+max);
        if (maxTextSize >= max) {
            maxTextSize = max;
        }
        minTextSize = min;
    }

    protected void refitText(String text, int textWidth) {

        if (textWidth > 0) {
            int availableWidth = textWidth - this.getPaddingLeft()
                    - this.getPaddingRight();
            float trySize = maxTextSize;

            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
            while ((trySize > minTextSize)
                    && (this.getPaint().measureText(text) > availableWidth)) {
                trySize -= 1;
                if (trySize <= minTextSize) {
                    trySize = minTextSize;
                    break;
                }
                this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
            }
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
        }
    }

    @Override
    protected void onTextChanged(final CharSequence text, final int start,
                                 final int before, final int after) {
        refitText(text.toString(), this.getWidth());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (w != oldw) {
            refitText(this.getText().toString(), w);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        refitText(this.getText().toString(), parentWidth);
    }

    public float getMinTextSize() {
        return minTextSize;
    }

    public void setMinTextSize(int minTextSize) {
        this.minTextSize = minTextSize;
    }

    public float getMaxTextSize() {
        return maxTextSize;
    }

    public void setMaxTextSize(int minTextSize) {
        this.maxTextSize = minTextSize;
    }

    protected float minTextSize;
    protected float maxTextSize;

}
